<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午4:41
 */
class Mgirl extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    /**
     * 获取人气女郎列表
     * @param $isHot
     * @param $pageSize
     * @param $limit
     * @param string $order
     * @return mixed
     */
    function getGirlAll($isHot=0,$pageSize,$limit=0,$order='tg.voteCount desc'){

        $where = 'tg.`status`=1';

        if($isHot){
            $where .= ' and tg.`is_hot`='.$isHot;
        }

        if($limit > 0){
            $limit = "limit 0,$limit";
            $sql = "select * from `tbgirl_user` as tu  join `tbpopular_girl` as tg on tu.`girl_id`=tg.`id` where {$where} order by $order $limit ";
            $query = $this->db->query($sql);
            $data['data'] = $query->result_array();
        }else{
            $sqlCount = $this->db->query("select * from `tbgirl_user` as tu  join `tbpopular_girl` as tg on tu.girl_id=tg.id where $where");
            $count = $sqlCount->num_rows();

            $sql = "select * from `tbgirl_user` as tu  join `tbpopular_girl` as tg on tu.`girl_id`=tg.`id` where {$where} order by $order limit ";

            $data = $this->db->pager($count,$pageSize,$sql);
        }



        //人气女郎
        $uidArr = array();
        foreach($data['data'] as $value){
            $uidArr[] = $value['uid'];
        }

        //人气女郎对应的用户
        if(!empty($uidArr)){
            $uId = implode(',',array_unique($uidArr));

            $sqlUser = "select `id`,`headurl` from `tbuserinfo` where `id` in($uId) ";
            $queryUser = $this->db->query($sqlUser);
            $headurlArr = array();
            foreach($queryUser->result() as $value){
                $headurlArr[$value->id] = $value->headurl;
            }
        }

        //合并人气女郎和推送用户uid
        foreach($data['data'] as &$value){
            $girlUid = isset($headurlArr[$value['uid']]) ? $headurlArr[$value['uid']] :0;
            if(!empty($girlUid)){
                $value['headurl'] = $girlUid;
            }
            //$value['uid'] = $headurlArr[$value['uid']]['uid'];

        }

        return $data;
    }

    function getGirlById($id)
    {
        $sql = "select * from `tbpopular_girl` where `id`=?";
        $query = $this->db->query($sql,array($id));
        $row = $query->row_array();

        if(!empty($row)){

            $sqlGirl = "select * from `tbgirl_user` where `girl_id`=".$row['id'];
            $queryGirl = $this->db->query($sqlGirl);

            $item = array();
            foreach($queryGirl->result() as $value){
                $item[] = $value->uid;
            }
            $row['uids'] = implode(',',$item);
        }

        return $row;
    }

}